package backtrack;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author : Rookie
 * @description : 子集 有重复元素 但结果中不能有重复的子集
 * @date : 2023/12/7
 */
public class SubSet2 {

    public static void main(String[] args) {
        int[] nums = new int[] {1,2,2};
        Arrays.sort(nums);
        List<List<Integer>> result = new ArrayList<>();
        backtrack(nums,0,new ArrayList<>(),result);
        System.out.println(result);
    }

    private static void backtrack(int[] nums, int startIndex, List<Integer> path,List<List<Integer>> result) {
        if (startIndex <= nums.length) {
            result.add(new ArrayList<>(path));
        } else {
            return;
        }

        for (int i = startIndex; i < nums.length; i++) {
            // 元素重复
            if (i>startIndex&& nums[i] == nums[i-1]) {
                continue;
            }
            path.add(nums[i]);
            backtrack(nums,i+1,path,result);
            path.remove(path.size()-1);
        }
    }
}
